<?php
class Courses extends CI_Model{

 	function __construct(){
    	parent::__construct();
	}

	/**
	 * 网课分类
	 * @param  integer $limit [description]
	 * @return [type]         [description]
	 */
	public function get_cate_list($limit = 3)
	{
		$sql = "select * from {$this->db->dbprefix}course_cate where type=1 and school_id=$this->school_id order by sort desc limit $limit";
		return $this->db->query($sql)->result_array();
	}

	/**
	 * 获取首页推荐课程
	 * @param  string $value [description]
	 * @return [type]        [description]
	 */
	public function get_recomend_course_list($limit=6)
	{
		// $where = " where cs.status=1 and c.status=1 and cs.type=1 ";
		$where = "where cs.status=1 and cs.school_id = $this->school_id and c.type = 1 ";
		$order = " order by cs.sort desc";
		$sql = "select c.*,t.teacher_name AS school_teacher_name,t.school_name, cs.fx_price,cs.is_free from {$this->db->dbprefix}course_shelves as cs left join {$this->db->dbprefix}course as c on(course_id = c.id) left join {$this->db->dbprefix}o2o_teachers as t on(c.teacher_id = t.id) $where $order limit $limit";
		return  $this->db->query($sql)->result_array();
	}
	
	

	function get_course_list($orders = 0,$cate = 0,$subject = 0,$limit = 0,$offset = 9){

		if($cate > 0){
			$cate = "and (c.cate_id = $cate or cs.cate_id = $cate)";
		}else{
			$cate = '';
		}
		if($subject > 0){
			$subject = 'and c.subject_id ='.$subject;
		}else{
			$subject = '';
		}
		$where = "where cs.status=1 and cs.school_id = $this->school_id and c.type = 1 $cate $subject";
		$order = " order by c.id desc";
		if($orders == 1){
			$order = " order by c.click_num desc";
		}elseif($orders == 2){
			$order = " order by c.price desc";
		}elseif($orders == 3){
			$order = " order by c.price asc";
		}
		$sql = "select SQL_CALC_FOUND_ROWS c.*,cs.fx_price,cs.is_free,cs.uid as fx_uid,t.teacher_name AS school_teacher_name,t.school_name from {$this->db->dbprefix}course_shelves as cs left join {$this->db->dbprefix}course as c on(course_id = c.id) left join {$this->db->dbprefix}o2o_teachers as t on(c.teacher_id = t.id) $where $order limit $limit,$offset";
		$result = $this->db->query($sql)->result_array();

		$row_num =  $this->db->query('SELECT FOUND_ROWS() as row_num;')->row_array();
		return array("count" => $row_num['row_num'], "result" => $result);
	}


	function check_is_buy($course_id,$type = 0){
		$sql = "select * from {$this->db->dbprefix}o2o_student_course where course_id = $course_id and course_type = $type order by id desc";
		$result = $this->db->query($sql)->row_array();
		return $result;
	}
	
}